package com.ruoyi.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.service.UserService;
import com.ruoyi.user.mapper.UserMapper;
import com.ruoyi.user.vo.LoginInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
* @author 付
* @description 针对表【t_user(用户表)】的数据库操作Service实现
* @createDate 2025-04-22 09:41:04
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public AjaxResult login(LoginInfoVo loginInfoVo) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("user_mobile", loginInfoVo.getUserMobile());
        User user = userMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(user)){
            return AjaxResult.error("用户不存在,请注册用户");
        }
        //判断密码是否正确
        if (!user.getUserPass().equals(loginInfoVo.getUserPass())){
            return AjaxResult.error("密码错误");
        }
        //登录成功
        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userName", user.getUserName())
                .claim("userMobile", user.getUserMobile())
                .claim("userId", user.getUid())
                .compact();
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("userName",user.getUserName());
        map.put("userMobile",user.getUserMobile());
        map.put("userId",user.getUid());
        map.put("user",user);
        //将用户信息保存到redis中
        stringRedisTemplate.opsForValue().set("user:login:"+user.getUserMobile(),user.getUserName());
        stringRedisTemplate.opsForValue().set("token:"+user.getUid(),token,60, TimeUnit.MINUTES);
        return AjaxResult.success(map);

    }

    @Override
    public AjaxResult register(User user) {
        if (ObjectUtils.isEmpty(user)){
            return AjaxResult.error("注册信息为空");
        }
        user.setRegisterTime(new Date());//注册时间

        userMapper.insert(user);//保存用户信息

        return AjaxResult.success("注册成功");
    }

    @Override
    public AjaxResult findPassword(User user) {
        // 判断手机号是否存在
        User user1 = userMapper.selectOne(new QueryWrapper<User>().eq("user_mobile", user.getUserMobile()));
        if (user1 == null) {
            return AjaxResult.error("手机号不存在");
        }
        //校验验证码
        String code = stringRedisTemplate.opsForValue().get("code:" + user.getUserMobile());
        if (!user.getCode().equals(code)) {
            return AjaxResult.error("验证码错误");
        }
        //修改密码
        user1.setUserPass(user.getUserPass());
        userMapper.updateById(user1);
        return AjaxResult.success("修改成功");
    }
}




